package com.thd.springboot.framework.utils;

import java.io.*;

public class ObjectUtils {
    public static <T> T clone(T source) throws IOException, ClassNotFoundException {
        //序列化源对象
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(source);
        //反序列化得到新对象
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        T target = (T) ois.readObject();
        ois.close();
        oos.close();
        return target;
    }
}
